package com.shuoyue.ycgk.ui.account;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.MemeryCatch;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.UserInfo;
import com.shuoyue.ycgk.ui.account.AccountContract;
import com.shuoyue.ycgk.ui.account.SmsContract;
import com.shuoyue.ycgk.utils.EditCheckUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class UpdatePwdActivity extends BaseMvpActivity<AccountContract.Presenter>
        implements AccountContract.View, SmsContract.View {

    @BindView(R.id.page_title)
    TextView pageTitle;
    //    @BindView(R.id.phone_num)
//    EditText phoneNum;
    @BindView(R.id.passwordold)
    EditText passwordold;
    @BindView(R.id.password1)
    EditText password1;
    @BindView(R.id.password2)
    EditText password2;
    SmsContract.Presenter smsPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_update_pwd;
    }

    @Override
    protected void initView() {
        pageTitle.setText("修改密码");
    }

    @Override
    public void initData() {
        mPresenter = new AccountContract.Presenter();
        mPresenter.attachView(this);
        smsPresenter = new SmsContract.Presenter();
        smsPresenter.attachView(this);
    }

    @Override
    public void loginSuc(UserInfo userInfo) {

    }

    @Override
    public void registSuc() {

    }

    @Override
    public void registFail(String msg) {

    }

    @Override
    public void resetSuc() {
        toast("密码已修改");
        MemeryCatch.token = "";
        SPUtils.clearUserPassword(mContext);
        SPUtils.clearUserInfo(mContext);
        finish();
    }

    @Override
    public void bindPhone(String openId, String accessToken) {

    }

    @OnClick({R.id.back, R.id.submit, R.id.go_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.submit:
                submit();
                break;
            case R.id.go_regist:
                startActivity(new Intent(mContext, ForgetPwdActivity.class));
                break;
        }
    }


    void submit() {
//        String phone = phoneNum.getText().toString();
        String pwdold = passwordold.getText().toString();
        String pwd1 = password1.getText().toString();
        String pwd2 = password2.getText().toString();
//        if (TextUtils.isEmpty(phone)) {
//            toast("请输入手机号码");
//            return;
//        }
        if (TextUtils.isEmpty(pwdold)) {
            toast("请输入原密码");
            return;
        }
        if (TextUtils.isEmpty(pwd1) || TextUtils.isEmpty(pwd2)) {
            toast("请输入密码");
            return;
        }
        if (!pwd1.equals(pwd2)) {
            toast("密码不一致");
            return;
        }
        if (pwd1.length() < 8) {
            toast("密码长度需大于等于8位");
            return;
        }
        if(!pwd1.matches("^(?:(?=.*[0-9].*)(?=.*[A-Za-z].*)(?=.*[\\W].*))[\\W0-9A-Za-z]{8,16}$")){
            toast("必须包含数字、英文字母、特殊符号");
            return;
        }
//        if (!pwd1.matches(".*[a-zA-z].*") ||
//                pwd1.matches(".*[0-9].*")) {
//            toast("密码必须包含字符和数字");
//            return;
//        }
//        if (pwd1.length() < 8) {
//            toast("密码长度需大于等于8位");
//            return;
//        }
        mPresenter.updatePwd(pwdold, pwd1);
    }

    /**
     * 发送验证码后
     */
    @Override
    public void sendSuc(String phone) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
